package com.shop.loan.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.shop.loan.mapper.EruptUserByMybatisMapper;
import com.shop.loan.model.common.Code;
import com.shop.loan.model.common.ResResult;
import com.shop.loan.model.entity.EruptUserByMybatis;
import com.shop.loan.model.enums.OpFailType;
import com.shop.loan.model.vo.AdminLoginVo;
import com.shop.loan.model.vo.OpPasswordUpdateVo;
import com.shop.loan.redis.RedisCache;
import com.shop.loan.redis.RedisUtil;
import com.shop.loan.service.EruptDataService;
import com.shop.loan.service.EruptUserDataService;
import com.shop.loan.service.SettingService;
import com.shop.loan.util.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import xyz.erupt.upms.service.EruptUserService;
import xyz.erupt.upms.util.IpUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Service
public class EruptDataServiceImpl implements EruptDataService {

    @Value("${sys.adminPhone}")
    private String adminPhone;

    @Resource
    SysMsgUtil sysMsgUtil;

    @Resource
    RedisUtil redisUtil;

    @Resource
    EruptUserByMybatisMapper eruptUserByMybatisMapper;

    @Resource
    OpFailUtil opFailUtil;

    @Resource
    private HttpServletRequest request;

    @Resource
    EruptUserService eruptUserService;

    @Resource
    EruptUserDataService eruptUserDataService;

    @Resource
    SettingService settingService;


    @Override
    public ResResult getMsgCode(AdminLoginVo adminLoginVo) {
        String redisPicCode = redisUtil.get(RedisCache.userLoginPicCode + adminLoginVo.getAccount());
        if (redisPicCode == null || !redisPicCode.trim().equals(adminLoginVo.getPicCode())) {
            return new ResResult(Code.PIC_CODE_INVALID);
        }

        EruptUserByMybatis eruptUserByMybatis = eruptUserByMybatisMapper.selectByAccount(adminLoginVo.getAccount());
        if (eruptUserByMybatis == null || eruptUserByMybatis.getId() == null) {
            return new ResResult(Code.ACCOUNT_NOT_EXIST);
        }
        if (!eruptUserByMybatis.getStatus()) {
            return new ResResult(Code.ACCOUNT_STATUS_ERROR);
        }
        if (!StringUtils.hasLength(eruptUserByMybatis.getPhone())) {
            return new ResResult(Code.ACCOUNT_MOBILE_NULL);
        }
        boolean isSuccess = sysMsgUtil.sendVerifyCode(eruptUserByMybatis.getPhone());
        if (isSuccess) {
            return new ResResult(Code.SUCCESS);
        }
        return new ResResult(Code.FAIL);
    }


    public ResResult getPicCode(AdminLoginVo adminLoginVo) {
        if (!StringUtils.hasLength(adminLoginVo.getAccount())) {
            return new ResResult(Code.PARAM_LACK);
        }
        ValidateCodeUtil.Validate validate = ValidateCodeUtil.getRandomCode();
        redisUtil.setex(RedisCache.userLoginPicCode + adminLoginVo.getAccount(), 300, validate.getValue());

        JSONObject dataJs = new JSONObject();
        dataJs.put("picCode", validate.getBase64Str());
        return new ResResult(Code.SUCCESS, dataJs);
    }


    @Override
    public ResResult getAdminCode(OpPasswordUpdateVo opPasswordUpdateVo) {
        boolean isSuccess = sysMsgUtil.sendVerifyCode(adminPhone);
        if (isSuccess) {
            return new ResResult(Code.SUCCESS);
        }
        return new ResResult(Code.FAIL);
    }

    @Override
    public ResResult resetLoanPassword(OpPasswordUpdateVo opPasswordUpdateVo) {
        if (!StringUtils.hasLength(opPasswordUpdateVo.getPassword()) || !StringUtils.hasLength(opPasswordUpdateVo.getCode())) {
            return new ResResult(Code.PARAM_LACK);
        }
        if(opPasswordUpdateVo.getPassword().length() < 6){
            return new ResResult(Code.PASSWORD_TOO_SHORT);
        }
        Long eruptUserId = eruptUserService.getCurrentUid();
        boolean checkStatus = sysMsgUtil.checkVerifyCode(adminPhone, opPasswordUpdateVo.getCode());
        if (!checkStatus) {
            opFailUtil.opFailOp(IpUtil.getIpAddr(request), eruptUserId, OpFailType.loan);
            return new ResResult(Code.PHONE_CODE_INVALID);
        }

        settingService.updateLoanPassword(opPasswordUpdateVo.getPassword());
        return new ResResult(Code.SUCCESS);
    }

    @Override
    public ResResult resetPayPassword(OpPasswordUpdateVo opPasswordUpdateVo) {
        if (!StringUtils.hasLength(opPasswordUpdateVo.getPassword()) || !StringUtils.hasLength(opPasswordUpdateVo.getCode())) {
            return new ResResult(Code.PARAM_LACK);
        }
        if(opPasswordUpdateVo.getPassword().length() < 6){
            return new ResResult(Code.PASSWORD_TOO_SHORT);
        }
        Long eruptUserId = eruptUserService.getCurrentUid();
        boolean checkStatus = sysMsgUtil.checkVerifyCode(adminPhone, opPasswordUpdateVo.getCode());
        if (!checkStatus) {
            opFailUtil.opFailOp(IpUtil.getIpAddr(request), eruptUserId, OpFailType.loan);
            return new ResResult(Code.PHONE_CODE_INVALID);
        }
        settingService.updatePayPassword(opPasswordUpdateVo.getPassword());
        return new ResResult(Code.SUCCESS);
    }
}
